package com.ruoyi.project.system.word.service;

import java.lang.reflect.Field;
import java.util.List;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.exception.ParamsException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.system.word.mapper.WordMapper;
import com.ruoyi.project.system.word.domain.Word;
import com.ruoyi.common.support.Convert;

/**
 * 系统文档管理 服务层实现
 *
 * @author lxs
 * @date 2018-10-17
 */
@Service
@SuppressWarnings("ALL")
public class WordServiceImpl implements IWordService {
    @Autowired
    private WordMapper wordMapper;

    /**
     * 查询系统文档管理信息
     *
     * @param id 系统文档管理ID
     * @return 系统文档管理信息
     */
    @Override
    public Word selectWordById(String id) {
        return wordMapper.selectWordById(id);
    }

    /**
     * 查询系统文档管理列表
     *
     * @param word 系统文档管理信息
     * @return 系统文档管理集合
     */
    @Override
    public List<Word> selectWordList(Word word) {
        return wordMapper.selectWordList(word);
    }

    /**
     * 新增系统文档管理
     *
     * @param word 系统文档管理信息
     * @return 结果
     */
    @Override
    public int insertWord(Word word) {
        word.preInsert(null);
        return wordMapper.insertWord(word);
    }

    /**
     * 修改系统文档管理
     *
     * @param word 系统文档管理信息
     * @return 结果
     */
    @Override
    public int updateWord(Word word) {
        Word wordNow = wordMapper.selectWordById(word.getId());
        if (wordNow != null) {
            if (wordNow.getStatus() != null && wordNow.getStatus() != 3) {
                handlePermission(word, wordNow);
                wordNow.preUpdate(null);
                return wordMapper.updateWord(wordNow);
            } else {
                throw new ParamsException(word.getLabel() + "该状态不能修改！");
            }
        } else {
            throw new ParamsException(word.getLabel() + "已被删除！");
        }
    }

    /**
     * 删除系统文档管理对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteWordByIds(String ids) {
        String[] idStr = Convert.toStrArray(ids);
        for (String id : idStr) {
            Word word = wordMapper.selectWordById(id);
            if (word != null) {
                if (word.getDel() == null || word.getDel() == 0) {
                    wordMapper.deleteWordById(id);
                } else {
                    throw new ParamsException(word.getLabel() + "禁止删除！");
                }
            }
        }
        return idStr.length;
    }

    @Override
    public int release(String id) {
        Word word = wordMapper.selectWordById(id);
        if (word != null) {
            if (word.getStatus() == null || word.getStatus() != 3) {
                return wordMapper.updateStatus(word.getId(), 3);
            } else {
                throw new ParamsException(word.getLabel() + "已发布！");
            }
        } else {
            throw new ParamsException("该记录已被删除！");
        }
    }

    @Override
    public int cancelRelease(String id) {
        Word word = wordMapper.selectWordById(id);
        if (word != null) {
            if (word.getStatus() != null && word.getStatus() == 3) {
                return wordMapper.updateStatus(word.getId(), 2);
            } else {
                throw new ParamsException(word.getLabel() + "已取消发布！");
            }
        } else {
            throw new ParamsException("该记录已被删除！");
        }
    }

    /**
     * 处理权限问题
     *
     * @param wordNew
     * @param wordNow
     */
    private void handlePermission(Word wordNew, Word wordNow) {
        String permission = wordNow.getPermission();
        Field[] fields = wordNow.getClass().getDeclaredFields();
        for (Field field : fields) {
            try {
                if (StringUtils.isBlank(permission) || !("#" + permission + "#").contains("#" + field.getName() + "#")) {
                    field.setAccessible(true);
                    field.set(wordNow, field.get(wordNew));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}
